﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace UFPC.Models
{
    public class UfpcRepository : IUfpcRepository
    {
        private UFPCDataContext _dataContext;

        public UfpcRepository()
        {
            _dataContext = new UFPCDataContext();
        }

        public IList<Order> ListOrders()
        {
            var orders = from o in _dataContext.Orders
                         select o;

            return orders.ToList();
        }

        public IList<Product> ListProducts()
        {
            var products = from p in _dataContext.Products
                          select p;

            return products.ToList();
        }

        public IList<Customer> ListCustomers()
        {
            var customer = from c in _dataContext.Customers
                           join a in _dataContext.Addresses
                           on c.AddressId equals a.AddressId
                           select c;

            return customer.ToList();
        }

        public IList<Inventory> ListInventories()
        {
            var inventory = from i in _dataContext.Inventories
                            join p in _dataContext.Products
                            on i.ProductId equals p.ProductId
                            select i;

            return inventory.ToList();
        }

        public IList<CustomerProductPrice> GetProductsByCustomer(int id)
        { 
            var productsByCustomer = from cpp in _dataContext.CustomerProductPrices
                                     join p in _dataContext.Products
                                     on cpp.ProductId equals p.ProductId
                                     where cpp.CustomerId == id
                                     select cpp;

            return productsByCustomer.ToList();
        }

        public IEnumerable<OrderDetail> OrderDetails(int id)
        {
            var orderDetails = from od in _dataContext.OrderDetails
                               where od.OrderId == id
                               select od;

            return orderDetails.ToList();
        }

        public Order OrderSummary(int id)
        {
            return _dataContext.Orders.SingleOrDefault(od => od.OrderId == id);
        }

        public Product ProductDetails(int id)
        {
            return _dataContext.Products.SingleOrDefault(p => p.ProductId == id);
        }

        public Customer CustomerDetails(int id)
        {
            return _dataContext.Customers.SingleOrDefault(c => c.CustomerId == id);            
        }

        public CustomerProductPrice CustomerProduct(int customerId, int productId)
        {
            return _dataContext.CustomerProductPrices.SingleOrDefault(cpp => (cpp.CustomerId == customerId && cpp.ProductId == productId));
        }

        public void Save()
        {
            _dataContext.SubmitChanges();
        }

        public void AddProduct(Product product)
        {
            _dataContext.Products.InsertOnSubmit(product);
        }

        public void AddProductToCustomer(CustomerProductPrice customerProduct)
        {
            _dataContext.CustomerProductPrices.InsertOnSubmit(customerProduct);
        }

        public void AddCustomer(Customer customer)
        {
            _dataContext.Customers.InsertOnSubmit(customer);
        }

        public void AddInventory(Inventory inventory)
        {
            _dataContext.Inventories.InsertOnSubmit(inventory);
        }

        public void AddOrder(Order order)
        {
            _dataContext.Orders.InsertOnSubmit(order);
        }

        public void AddOrderDetail(OrderDetail orderDetail)
        {
            _dataContext.OrderDetails.InsertOnSubmit(orderDetail);
        }

        public void DeleteCustomer(Customer customer)
        {
            _dataContext.Customers.DeleteOnSubmit(customer);
        }

        public void DeleteOrder(Order order, IEnumerable<OrderDetail> orderDetails)
        {
            _dataContext.Orders.DeleteOnSubmit(order);
            _dataContext.OrderDetails.DeleteAllOnSubmit(orderDetails);
        }

        public void DeleteProduct(Product product)
        {
            _dataContext.Products.DeleteOnSubmit(product);
        }

        public void DeleteProduct(CustomerProductPrice customerProduct)
        {
            _dataContext.CustomerProductPrices.DeleteOnSubmit(customerProduct);
        }
    }
}